﻿using System.Collections.Generic;
using System.Globalization;
using System.Web;
using Com.Alipay;

namespace HM.PayService
{
    public class EPay
    {
        //支付宝配置
        public const string AliKey = "e7wppquhtqi4jjyrbvwzijpk0lb0j5lu";
        public const string AliPartner = "2088621731317106";
        public const string AlipayAccount = "1952242804@qq.com";
        //订单回调地址
        public const string AlipayNotifyUrl = "http://www.xiaomishq.com/Pay/NotifyHandle";
        public const string AlipayReturnUrl = "http://www.xiaomishq.com/Pay/ReturnHandle";
        //public const string AlipayNotifyUrl = "http://www.xiaomishq.com:8080/Pay/NotifyHandle";
        //public const string AlipayReturnUrl = "http://www.xiaomishq.com:8080/Pay/ReturnHandle";
        //充值回调地址
        public const string AlipayNotifyUrlRecharge = "http://www.xiaomishq.com/Pay/NotifyHandleRecharge";
        public const string AlipayReturnUrlRecharge = "http://www.xiaomishq.com/Pay/ReturnHandleRecharge";
        //public const string AlipayNotifyUrlRecharge = "http://www.xiaomishq.com:8080/Pay/NotifyHandleRecharge";
        //public const string AlipayReturnUrlRecharge = "http://www.xiaomishq.com:8080/Pay/ReturnHandleRecharge";


        public string CheckOut(HttpContextBase httpContext, string outTradeNo, string totalFee, string _orderName = "", string body = "众享经济链")
        {
            var back = GoAlipay(httpContext, outTradeNo,"商品支付的备注title", totalFee, AlipayReturnUrl, AlipayNotifyUrl, body);

            return back;
        }

        public string Recharge(HttpContextBase httpContext, string outTradeNo, string totalFee, string body = "采霖源")
        {
            var back = GoAlipay(httpContext, outTradeNo, "商品支付的备注title", totalFee, AlipayReturnUrlRecharge, AlipayNotifyUrlRecharge, body);

            return back;
        }

        /// <summary>
        /// 支付宝支付
        /// </summary>
        /// <param name="context"></param>
        /// <param name="outTradeNo">订单编号</param>
        /// <param name="name">订单名称</param>
        /// <param name="totalFee">付款金额</param>
        /// <param name="notifyUrl"></param>
        /// <param name="body">订单描述</param>
        /// <param name="showUrl">商品展示地址</param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public string GoAlipay(HttpContextBase context, string outTradeNo, string name, string totalFee, string returnUrl, string notifyUrl, string body = "", string showUrl = @"http://www.cailinyuan.com/Home/Center")
        {
            ////////////////////////////////////////////请求参数////////////////////////////////////////////
            Config.Key = AliKey;
            Config.Partner = AliPartner;
            //支付类型
            const string paymentType = "1";
            //防钓鱼时间戳
            string antiPhishingKey = Submit.Query_timestamp();
            //若要使用请调用类文件submit中的query_timestamp函数

            //客户端的IP地址
            string exterInvokeIp = context.Request.UserHostAddress;
            //非局域网的外网IP地址，如：221.0.0.1


            ////////////////////////////////////////////////////////////////////////////////////////////////

            //把请求参数打包成数组
            var sParaTemp = new SortedDictionary<string, string>
                {
                    {"partner", AliPartner},
                    {"_input_charset", Config.Input_charset.ToLower()},
                    {"service", "create_direct_pay_by_user"},
                    {"payment_type", paymentType},
                    {"notify_url", notifyUrl},
                    {"return_url", returnUrl},
                    {"seller_email", AlipayAccount},
                    {"out_trade_no", outTradeNo},
                    {"subject", name},
                    {"total_fee", totalFee},
                    {"body", body},
                    {"show_url", showUrl},
                    {"anti_phishing_key", antiPhishingKey},
                    {"exter_invoke_ip", exterInvokeIp}
                };

            //建立请求
            string sHtmlText = Submit.BuildRequest(sParaTemp, "get", "确认");
            return sHtmlText;
        }
    }

}